package com.tang.algorithm.string;

/**
 * @author tlm
 * @Description
 * @date 2020/6/22 19:52 下午
 */
public class LeetCode67 {
    public static void main(String[] args) {
        System.out.println(addBinary("11", "1"));
//        System.out.println(3/2);
    }


    public static String addBinary(String a, String b) {
        StringBuilder res = new StringBuilder();
        //进位
        int carry = 0;
        for (int i = a.length() - 1, j = b.length() - 1; i >= 0 || j >= 0; i--, j--) {
            int sum = carry;
            //把每个字符串上的字符转为数字，如果不存在，置为0
            sum += i >= 0 ? a.charAt(i) - '0' : 0;
            sum += j >= 0 ? b.charAt(j) - '0' : 0;
            res.append(sum%2);
            carry=sum/2;

        }
        res.append(carry==1?carry:"");
        return res.reverse().toString();
    }
}
